package kp.rpc.fault.tolerant.factory;

import kp.rpc.fault.retry.RetryStrategy;
import kp.rpc.fault.retry.impl.NoRetryStrategy;
import kp.rpc.fault.tolerant.TolerantStrategy;
import kp.rpc.fault.tolerant.impl.FailFastTolerantStrategy;
import kp.rpc.spi.SpiLoader;

/**
 * @author tzf
 * @description: 容错策略工厂
 * @date: 2024/6/7 14:17
 */

public class TolerantStrategyFactory {
    static {
        SpiLoader.load(TolerantStrategy.class);
    }

    //默认重试器
    private final static TolerantStrategy DEFAULT_RETRY_STRATEGY = new FailFastTolerantStrategy();

    public static TolerantStrategy getInstance(String key) {
        Object instance = SpiLoader.getInstance(TolerantStrategy.class, key);
        return instance == null ? DEFAULT_RETRY_STRATEGY : (TolerantStrategy) instance;
    }
}
